#ifndef BACTERIALCONTIGBROWSER_H
#define BACTERIALCONTIGBROWSER_H

#include <QMainWindow>
#include <QGraphicsWidget>
#include <QString>
#include <QDebug>
#include <QRectF>
#include <QPainterPath>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QWidget>
#include <QHash>
#include <QList>
#include <QPointer>
#include <QGraphicsLinearLayout>
#include <QGraphicsSceneWheelEvent>
#include <QFile>
#include <qfile.h>
#include <QKeyEvent>
#include <QtAlgorithms>
#include <QDir>
#include <QFileDialog>
#include <QGraphicsProxyWidget>
#include <QRubberBand>
#include <QProcess>
#include <QThread>

#include "../../visbasewidget.h"
#include "contig.h"
#include "../../resizerect.h"
#include "brush.h"
#include "contigbrowsermenubar.h"
#include "exporter.h"
#include "genomedatauploader.h"
#include "genomefactory.h"
//#include "../../movingpoint.h"
//#include "gene.h"

//this class is my first attempt at visualizing lots of bacterial genome contigs
//I am torn about the name- to some extent this might be generalized to work for any
//situation where you want to show LOTs of genomes in a stacked view- with ability to align
//sort and filter.  But, for now I will focus on the specific task until it becomes clear
//whether this can be used as a general purpose genome fragment viewer
class BacterialContigBrowser : public VisBaseWidget
{
    Q_OBJECT

public:
    explicit BacterialContigBrowser(SageVis *svp, QGraphicsItem *parent = 0);
    ~BacterialContigBrowser();

    //these allow elts to be drawn
//    QRectF boundingRect() const;
    QPainterPath shape() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
               QWidget *widget);

    bool loadData( QString pathToFile );
    bool loadData( QString gffFile, QString fastaFile );
    bool load( QString gffFile, QString fastaFile);
    bool loadData2( QString pathToFile );
    QColor* backgroundColor;

//protected:
//    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
signals:
    void timeout();

public slots:
    void wheelEvent(QGraphicsSceneWheelEvent *event);
    void keyPressEvent ( QKeyEvent * event );

    void createdReformattedFileSlot(QString);//this is called when the data uploader creates the reformatted file
    void createContigsSlot();//this is called once the data uploader is finished, it starts the genomeFactory
    void layoutContigsSlot(QList<QPointer<Contig> >*);//this is called when contigs have been created and will arrange them in space

    void exportSelectedGenesSlot();
    void updateProgressBar(int);

    void advance();

    void sortSlot(int sortType);

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

private:
    bool readFastaOutputCdHitCluster( QString inputFastaFile, QString outputClusterFile );
    bool readClusterAndGFFWriteTabDelim( QString clusterInputFile, QString gffInputFile, QString outputTabDelimFile );
    bool readTabDelimFile( QString pathToFile );
    bool readReformatedFile( QString pathToFile );

    //not using
    void layoutContigs();

    //still using
    void setContigHeights( float newHeight ); // not using -- need to think about it more
    void positionContigs();
    void connectContigs();

    void repositionContigsAroundContigN(int contigN);

    QList < QPointer< Contig > > contigs;  //right now this stores QGraphicsWidgets- contigs
                                //I think I might eventually make a class called' visBaseElement'
                                    //which would store the subelements that make up a visWidget
                                    //but I'm not sure ...

    //these variables control display of contigs
    int startPosOfContigs;
    float deltaY;
    int maxNTPerContig; //this stores the longest contig on display- should result in uniform scale for genes in contigs
    float maxContigHeight;
    float minContigHeight;
    float defaultContigHeight;// height of contigs, if not enlarged or minimized
    int startContig; //the contig at the top of the page.  Initially contig 0, will shift as user navigates in scene

    int maxTextWidth;

    bool init; //this stores whether browser has been initialized

    QPointF mousePressStartPt;
    bool clickAndDragInitialized;
    QRectF highlightRect;
    //Not using this yet
//    QStringList allGenes;//list of genes in the data set
//    QStringList genesOnScreen;//list of the genes currently on display

    //Gene g;

    QList<Brush*> brushes;
    QList<QGraphicsProxyWidget*> brushProxyWidgets;
    QPointF* rubberBandOrigin;

    QString* gffFileName;
    QString* fastaFileName;
    QString* clusterFileName;
    QString reformattedFile;
    QProcess* cdHit; //this process generates the gene cluster ids on which coordinated
                    //highlighting and alignment are based

    ContigBrowserMenuBar* menuBar;

    QStringList selectedClusters;


};


////sorting
//bool contigLengthLessThan( const Contig &c1, const Contig &c2);


#endif // BACTERIALCONTIGBROWSER_H
